﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Template.CodeGenerator
{
   public static class ArrayExtensions
    {

        public static SqlInResult ToSqlIn(this string[] values,string parameterName, DynamicParameters parameters, string parameterPrefix="@")
        {
            if (parameters == null)
            {
                parameters = new DynamicParameters();
            }
            string inSql = "(";
            if (values != null && values?.Count() > 0)
            {
                int count = 0;
                
                foreach (var value in values)
                {
                    var newParameterName = $"{parameterName}{count}";
                    parameters.Add(newParameterName, value);
                    inSql = $"{inSql}{parameterPrefix}{newParameterName},";
                    count = count + 1;
                }
            }
            inSql = inSql.TrimEnd(",".ToCharArray());
            inSql = $"{inSql})";

            return new SqlInResult()
            {
                DynamicParameters = parameters,
                Sql = inSql,
            };
        }
    }


    public class SqlInResult
    {

        public DynamicParameters DynamicParameters { get; set; }

        public string Sql { get; set; }
    }
}
